var millName; //染厂名称
var millData = [];
var openID;
var hgS2;
var Judge_plan;
var Judge_Delete=false;    //是否可以删除其他人员的权限
var number=0;  //用来接收双击事件执行的次数
var code = getQueryString("code");

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}


$(function () {

    getopenid();
    showColor();

    $('#waterdetai').datagrid({
        onDblClickRow: function (rowIndex) {//鼠标双击事件
            console.log('进入双击事件');
            number=number+1;
            console.log(number);
            if (number==1){
                if (Judge_Delete) {
                    // $("#waterdetai").datagrid("selectRow", rowIndex);//选中此行
                    var currentRow = $("#waterdetai").datagrid("getSelected");//获得选中行的信息
                    console.log(currentRow.openid);
                    if(currentRow.openid !== openID) {
                        //不可以自己删除自己
                        //$.messager.alert('警告','警告消息');
                        $.messager.defaults = {ok: "确定", cancel: "取消", width: 250, closable: false};
                        $.messager.confirm('确认', '您确认要删除 ' + currentRow.WXname + ' 吗？', function (r) {
                            if (r) {
                                console.log('确认删除');
                                deletePeople(currentRow.openid);
                                //setTimeout(" getData();","1000");

                                number = 0;
                            } else {
                                number = 0;
                            }
                        });
                    }

                };
            }
        }
    });

});


function getopenid() {
    console.log(
        'https://www.xdfznh.club/wxpadbe/switch/oao/'
        + code);

    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe/switch/oao/'
            + code,
        type: 'get',
        dataType: 'json',
        // 成功回调
        success: function (data) {
            console.log('请求成功');
            openID = data.data;
            console.log('openID=' + openID);
            getmill();
        },
        //失败回调
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //查看错误信息
            console.log('失败回调');
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }
    })
}

//获取微信号对应的染厂名称
function getmill() {
    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe'
            + '/userMillController/selectMills/'
            + openID,

        type: 'get',
        dataType: 'json',
        success: function (data) {
            console.log(data.length);
            var ID;
            var liNodes = document.getElementsByTagName("input");
            if (data.length == 0) {
                // 关闭当前页面
                alert('当前用户没有权限！');

            }

            if (data.length > 0) {
                millName = data[0].mill;

                for (var n = 0; n < data.length; n++) {
                    millData[n] = data[n].mill;
                    console.log(data[n].mill);
                }
                ;
                getJurisdiction(); //获取个人查看报表的权限
                getDropDownDate(); // 设置下拉

            }
            ;
        }
    })
};

//获取个人查看报表的权限
function getJurisdiction() {
    console.log('https://www.xdfznh.club/wxpadbe'
        + '/perm/selectBoss/'
        + openID + '/'
        + millName);
    console.log(millName);

    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe'
            + '/perm/selectBoss/'
            + openID + '/'
            + millName,

        type: 'get',
        dataType: 'json',
        success: function (data) {

            if (data.length > 0) {
                millName = data[0].mill;
                var plan = eval('(' + data[0].auth_report + ')');
                Judge_Delete=eval('(' + data[0].perm_delete + ')')
                if (plan) {
                    Judge_plan = true;
                    //Judge_Delete = true;
                    getData();
                } else {
                    Judge_plan = false;
                    //Judge_Delete = false;
                    alert('当前用户没有查看排单计划权限！');
                }


                for (var n = 0; n < data.length; n++) {
                    millData[n] = data[n].mill;
                    console.log(data[n].mill);
                }
                ;
                // console.log(millData);
                getDropDownDate(); // 设置下拉

            } else {
                Judge_plan = false;
                alert('当前用户没有查看报表权限！');
            }
            ;
        }
    })

}


function getData() {
    console.log('https://www.xdfznh.club/wxpadbe/dynamicDS/query'
        + "/call AdministratorTable('" + millName + "')");

    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe/dynamicDS/query'
            + "/call AdministratorTable('" + millName + "')",

        type: 'get',
        dataType: 'json',
        success: function (data) {

            $('#waterdetai').datagrid('loadData', data);
            showColor();

        }, complete: function (XMLHttpRequest, textStatus) {
            console.log("进入到complete");
            showColor();
        }

    })
}


/******************************************************************************************/


//设置染厂下拉
function getDropDownDate() {
    console.log('2=' + millData);
    hgS2 = new selectSwiper({
        el: '.select_box2',
        mustSelect: true,
        activeIndex: 0,
        data: millData,
        init: function (index) {
            if (index !== -1) {
                $('.btn2').html(this.data[index]);
            }
        },
        okFunUndefind: function () {
            alert('必须选择一项');
        },
        okFun: function (index) {
            $('.btn2').html(this.data[index]);
            millName = $('button').text();
            getJurisdictionmill();

        },
        closeFun: function () {
            console.log('取消');
        },
    });

    $('.btn2').on('click', function () {
        hgS2.openSelectSwiper();

    });
}

//在更换染厂之后  重新刷新数据  并判断个人权限
function getJurisdictionmill() {
    console.log('https://www.xdfznh.club/wxpadbe'
        + '/perm/selectBoss/'
        + openID + '/'
        + millName);

    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe'
            + '/perm/selectBoss/'
            + openID + '/'
            + millName,

        type: 'get',
        dataType: 'json',
        success: function (data) {

            if (data.length > 0) {
                var plan = eval('(' + data[0].auth_report + ')');

                if (plan) {
                    Judge_plan = true;
                    getData();
                } else {
                    Judge_plan = false;
                    alert('当前用户没有查看排单计划权限！');
                }

            } else {
                Judge_plan = false;
                alert('当前用户没有查看报表权限！');
            }
            ;
        }
    })

}

function showColor() {
    $('#waterdetai').datagrid({
        rowStyler: function (index, row) {
            if ((index % 2) == 1) {
                //return 'color:#0080FF ';   //#E0ECFF
                return 'background-color:#E0ECFF';
                //return 'background-color:#0000C6;color:#fff;font-weight:bold;';
            }
        }
    });
}


//删除选中员工所有权限
function deletePeople(openid) {


    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe/perm/delete/'+ openid,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            getData()
        }
    })
}